package com.cskaoyan.controller.vx;

import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.brand.LoginUserVxData;
import com.cskaoyan.bean.brand.UserIndexVO;
import com.cskaoyan.bean.brand.UserInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.VxUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@ResponseBody
@Controller
@RequestMapping("wx")
public class AuthVxController {
    @Autowired
    VxUserService vxUserService;

    @RequestMapping("auth/login")
    public BaseRespVo login(@RequestBody Map map){
        String username = (String)map.get("username");
        String password = (String)map.get("password");

        String host = "wx";
        LoginUserVxData loginUserVxData = vxUserService.login(username, password,host);
//        LoginUserVxData loginUserVxData = new LoginUserVxData();
//        UserInfo userInfo = new UserInfo();
//        userInfo.setNickName(username);
//        loginUserVxData.setUserInfo(userInfo);
//        loginUserVxData.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIG1hcmtldCB0b2tlbiIsImF1ZCI6Ik1JTklBUFAiLCJpc3MiOiJNQVJLRVQiLCJleHAiOjE2NjI4ODg3NzAsInVzZXJJZCI6MjEsImlhdCI6MTY2Mjg4MTU3MH0.-XqfS4U85F4Z1tZkKFEDdI6TA5gOrQ9XOsTmHZ1oIH8");
//
//        BaseRespVo ok = BaseRespVo.ok(loginUserVxData);
//        ok.setErrmsg("成功");
        //Object primaryPrincipal = SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        if (loginUserVxData == null) {
             return BaseRespVo.erron1();
        }
        BaseRespVo ok = BaseRespVo.ok(loginUserVxData);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("auth/logout")
    public BaseRespVo logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        BaseRespVo ok = BaseRespVo.ok(null);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("user/index")
    public BaseRespVo index(){
        UserIndexVO date = vxUserService.index();
        BaseRespVo ok = BaseRespVo.ok(date);
        ok.setErrmsg("成功");
        return ok;
    }
}
